package  
{
	import org.flixel.*;
	
	/**
	 * ...
	 * @author Jay Wagner
	 */
	public class CreditsState extends FlxState
	{
		[Embed(source = "../data/txtFiles/credits.txt", mimeType = "application/octet-stream")]private var _creditsText:Class;
		
		private var _creditsString:String = new _creditsText;
		
		private var _nutsAndVoltsLogo:FlxSprite;
		private const _negativeSpaceTop:uint = 60;
		private const _negativeSpaceBottom:uint = 180;
		
		//private var _gameTitle:FlxText;
		private var _credits:FlxText;
		private var _endThanks:FlxText;
		//private var _titleBuffer:Number = 50; //sets the distance between the game title and the credits
		private var _scrollSpeed:Number = 1.2; //sets the scroll speed
		private var _exitable:Boolean;
		private var _fromExtras:Boolean;
		
		private const _buttonWidth:uint = 100;
		private const _buttonHeight:uint = 26;
		
		private var _mainMenuButton:FlxButtonPlus;
		private var _menuButtonText1:FlxText;
		private var _menuButtonText2:FlxText;
		private var _mButton1:FlxSprite;
		private var _mButton2:FlxSprite;
		private var _buttonPressed:Boolean = false;
		
		private var _musicFadeOutTrigger:Boolean = true;
		
		private var _songInformation:FlxButtonPlus;
		
		private const _creditsMusicVolume:Number = 0.75;
		
		private var _fadeInTimer:Number = 0.0;
		private const _fadeInMaxTime:Number = 1.0;
		
		private var _fadeOut:Boolean = false;
		private var _fadeSprite:FlxSprite;
		private var _fadeCounter:Number = 0.0;
		private const _fadeTimeMax:Number = 1.0;
		
		public function CreditsState(exitable:Boolean = true, fromExtras:Boolean = false):void
		{
			_exitable = exitable;
			_fromExtras = fromExtras;
		}
		
		override public function create():void 
		{
			_nutsAndVoltsLogo = new FlxSprite(0, 0, ImageFiles.nutsAndVolts);
			
			_credits = new FlxText(0, 15, FlxG.width, _creditsString);
			_credits.setFormat("PowellAntique", 20, 0xffff00, "left");
			
			_endThanks = new FlxText(0, 0, FlxG.width, "Thank you for playing!");
			_endThanks.setFormat("PowellAntique", 24, 0xffff00, "center");
			
			_nutsAndVoltsLogo.y = FlxG.height + _negativeSpaceTop;
			_credits.y = _nutsAndVoltsLogo.y + _nutsAndVoltsLogo.height - _negativeSpaceBottom - _negativeSpaceTop;
			_endThanks.y = _credits.y + _credits.height + 20;
			
			add(_nutsAndVoltsLogo);
			add(_credits);
			add(_endThanks);
			
			if (_fromExtras)
			{
				_mainMenuButton = new FlxButtonPlus(FlxG.width - _buttonWidth, FlxG.height - _buttonHeight, toMainMenu);
				_menuButtonText1 = new FlxText(0, 0, _buttonWidth, "Back").setFormat("PowellAntique", 15, 0xDDDDDD, "center", 0xFF000000);
				_menuButtonText2 = new FlxText(_menuButtonText1.x, _menuButtonText1.y, _menuButtonText1.width, _menuButtonText1.text).setFormat("PowellAntique", 15, 0xFFFFFF, "center", 0xFF000000);
			}
			else
			{
				_mainMenuButton = new FlxButtonPlus(FlxG.width - _buttonWidth, FlxG.height - _buttonHeight, toMainMenu);
				_menuButtonText1 = new FlxText(0, 0, _buttonWidth, "Main Menu").setFormat("PowellAntique", 15, 0xDDDDDD, "center", 0xFF000000);
				_menuButtonText2 = new FlxText(_menuButtonText1.x, _menuButtonText1.y, _menuButtonText1.width, _menuButtonText1.text).setFormat("PowellAntique", 15, 0xFFFFFF, "center", 0xFF000000);
			}
			_mButton1 = new FlxSprite().loadGraphic(ImageFiles.button);
			_mButton2 = new FlxSprite().loadGraphic(ImageFiles.buttonHighlight);
			_mainMenuButton.loadGraphic(_mButton1, _mButton2);
			_mainMenuButton.loadText(_menuButtonText1, _menuButtonText2);
			_mainMenuButton.setMouseOverCallback(mouseOverSound);
			if (_exitable)
			{
				add(_mainMenuButton);
			}
			
			_songInformation = new FlxButtonPlus(FlxG.width - 100, 0, openSongWebPage, null, "Livewire - Zef");
			_songInformation.setMouseOverCallback(mouseOverSound);
			add(_songInformation);
			
			_fadeSprite = new FlxSprite().createGraphic(640, 480, 0xff000000);
			_fadeSprite.alpha = 1;
			add(_fadeSprite);
			
			FlxG.playMusic(SoundFiles.creditsMusic, _creditsMusicVolume);
		}
		
		override public function update():void 
		{
			if (_fadeInTimer < _fadeInMaxTime && !_buttonPressed)
			{
				_fadeInTimer += FlxG.elapsed;
				
				_fadeSprite.alpha = Math.abs(1 - (_fadeInTimer / _fadeInMaxTime));
			}
			
			_nutsAndVoltsLogo.y -= _scrollSpeed;
			_credits.y -= _scrollSpeed;
			//_gameTitle.y -= _scrollSpeed;
			
			//Moves the thanks up until it reaches the middle of the screen
			if (_endThanks.y > FlxG.height / 2 - 10)
			{
				_endThanks.y -= _scrollSpeed;
			}
			
			_fadeOut = areCreditsDone();
			//if (areCreditsDone())
			//{
				//FlxG.state = new MainMenuState;
			//}
			
			if (_fadeOut || _buttonPressed)
			{
				_fadeCounter = _fadeSprite.alpha / _fadeTimeMax;
				_fadeCounter += FlxG.elapsed;
				
				if (_fadeSprite.alpha < _fadeCounter / _fadeTimeMax)
				{
					_fadeSprite.alpha = _fadeCounter / _fadeTimeMax;
				}
				
				if (_musicFadeOutTrigger)
				{
					FlxG.music.fadeOut(1.0);
					_musicFadeOutTrigger = false;
				}
				
				if (_fadeCounter >= _fadeTimeMax)
				{
					if (!_fromExtras)
					{
						FlxG.state = new MainMenuState(false);
					}
					else
					{
						FlxG.state = new ExtrasState();
					}
				}
			}
			
			super.update();
		}
		
		public function areCreditsDone():Boolean
		{
			if (Math.abs(_credits.y) > _credits.height + 10)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		private function toMainMenu():void
		{
			if (!_buttonPressed)
			{
				_buttonPressed = true;
			}

		}
		
		private function openSongWebPage():void
		{
			FlxU.openURL("https://8bc.org/members/Zef/");
		}
		
		private function mouseOverSound():void
		{
			FlxG.play(SoundFiles.mouseOverSound, 0.75);
		}
		
	}

}